<?php

$errors = array();

if ($_POST) {
	// run the validation script
	require_once('library.php');
	try {
		
		// Validate containerName name not empty
		$val = new Zend_Validate();
		$length = new Zend_Validate_StringLength(1,150);
		$val->addValidator($length);
		if (!$val->isValid($_POST['containerName'])){
			$errors['containerName'] = '*** Container name must be 1 to 150 characters -> ';
			}else{
				$sql = 'SELECT name from containers WHERE name = ? AND parentId = ?';
				$result = $dbRead->fetchAll($sql,array($_POST['containerName'],$_POST['parentId']));
				if ($result){
					$errors['containerName'] = '*** '.$_POST['containerName'] . ' is already in use -> ';
				}
			}	
		
		
		
		
		
		
		
		
		
		
		// Validate capacity name not empty
		$val = new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::STRING);
		if(!$val->isValid($_POST['capacity'])){
			$errors['capacity']='*** Required field -> ';
		}
		
		if(!$errors){
			$data = array('accountId' => $_SESSION['accountId'],
			              'dbId' => $_POST['dbId'],
						  'parentId' => $_POST['parentId'],
			              'name' => $_POST['containerName'],
			              'description' => $_POST['description'],
						  'colorCode' => $_POST['colorCode'],
						  'capasity' => $_POST['capacity'],);
			$dbWrite->insert('containers', $data);
			if($_POST['parentId']==0){
				header('Location: DBMain.php?dbId='.$_POST['dbId'].'&name='.$_GET['name']);
			}else{
				header('Location: DBMain.php?id='.$_POST['parentId'].'&dbId='.$_POST['dbId'].'&name='.$_GET['name']);
			}		  
		}
		
	} catch (Exception $e){
		echo $e->getMessage();
	} 
	
	
}
